package com.zjeav.lingjiao.ui.home.persenter;

import com.zjeav.lingjiao.base.baseBean.Book;
import com.zjeav.lingjiao.base.baseBean.Find;
import com.zjeav.lingjiao.base.baseBean.Result;
import com.zjeav.lingjiao.base.listener.CommonListener;
import com.zjeav.lingjiao.base.request.BookRequest;
import com.zjeav.lingjiao.base.request.FindRequset;
import com.zjeav.lingjiao.base.response.BannerResponse;
import com.zjeav.lingjiao.base.response.BookDetailResponse;
import com.zjeav.lingjiao.base.response.BooksResponse;
import com.zjeav.lingjiao.base.response.FindResponse;
import com.zjeav.lingjiao.base.response.UserBuyRecordResponse;
import com.zjeav.lingjiao.base.service.AssistBookService;
import com.zjeav.lingjiao.base.service.BannerService;
import com.zjeav.lingjiao.base.service.BuyHistoryService;
import com.zjeav.lingjiao.base.service.FindService;
import com.zjeav.lingjiao.base.service.HotBookService;
import com.zjeav.lingjiao.base.service.MallBannerService;
import com.zjeav.lingjiao.base.tools.ObservableServiceTool;
import com.zjeav.lingjiao.base.tools.RetrofitInstance;
import com.zjeav.lingjiao.ui.book.BookDetailContract;
import com.zjeav.lingjiao.ui.book.BookDetailModel;
import com.zjeav.lingjiao.ui.book.MallBookContract;
import com.zjeav.lingjiao.ui.home.model.HomeModel;
import com.zjeav.lingjiao.ui.home.view.BookMarkContract;
import com.zjeav.lingjiao.ui.home.view.FindContract;
import com.zjeav.lingjiao.ui.home.view.HomeContract;

import java.util.ArrayList;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class MainPersenter implements HomeModel.OnMainListener, BookDetailModel.OnGetBookDetailListener, CommonListener {
    HomeContract.View view;
    BookDetailContract.View bookview;
    HomeModel model;
    BookDetailModel bookDetailModel;
    BookMarkContract.View bookMarkView;
    FindContract.View findview;
    BookMarkContract.MallBannerView mallBannerView;
    int mType;//接口请求的类型

    public MainPersenter(BookDetailContract.View bookview) {
        this.bookview = bookview;
        this.bookDetailModel = new BookDetailModel();
    }

    public MainPersenter(HomeContract.View view) {
        this.view = view;
        this.model = new HomeModel();

    }

    public MainPersenter(FindContract.View view) {
        this.findview = view;
    }

    public MainPersenter(BookMarkContract.View bookMarkView) {
        this.bookMarkView = bookMarkView;
    }

    public void getMyBooks(BookRequest request) {
        this.model.getMyBooks(request, this);
    }
    public void getBuyHistory(int id,BookRequest request){
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        BuyHistoryService service = retrofit.create(BuyHistoryService.class);
        ObservableServiceTool.getResult((Observable) service.getBuyHistory(id,request), 4, this);
    }

    public void getHomeBanners() {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        BannerService service = retrofit.create(BannerService.class);
        ObservableServiceTool.getResult((Observable) service.getBanners(), 3, this);
    }
    public void getMarketBanner(){
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        MallBannerService service = retrofit.create(MallBannerService.class);
        ObservableServiceTool.getResult((Observable) service.getMallBanners(), 5, this);
    }

    public void getFinds(FindRequset requset) {
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        FindService service = retrofit.create(FindService.class);
        ObservableServiceTool.getResult((Observable) service.getFinds(requset), 2, this);
    }

    public void getHotBooks() {
        this.mType = 0;
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        HotBookService service = retrofit.create(HotBookService.class);
        ObservableServiceTool.getResult((Observable) service.getHotBooks(), 0, this);
    }

    public void getExtraBooks() {
        this.mType = 1;
        Retrofit retrofit = RetrofitInstance.getJsonTokenInstance();
        AssistBookService service = retrofit.create(AssistBookService.class);
        ObservableServiceTool.getResult((Observable) service.getAssistBooks(), 1, this);
    }

    public void getBookDetail(int id) {
        this.bookDetailModel.getBookDetail(id, this);
    }

    @Override
    public void OnMyBooksSuccess(Result<BooksResponse> result) {
        this.view.showMyBooks(result.getData().getExtra(), result.getData().getData());
    }

    @Override
    public void OnError(Throwable throwable) {
        this.view.ShowError(throwable);
    }

    @Override
    public void OnGetDetailSuccess(BookDetailResponse detailResponse) {
        this.bookview.ShowBookDetail(detailResponse);
    }

    @Override
    public void OnGetDetailFail(Throwable e) {
        this.bookview.ShowFail(e);
    }

    @Override
    public void OnSuccess(Result result, int type) {
        if (type == 0) {
            ArrayList<Book> booksResponse = (ArrayList<Book>) result.getData();
            this.bookMarkView.showHotBooks(booksResponse);
        } else if (type == 1) {
            ArrayList<Book> booksResponse = (ArrayList<Book>) result.getData();
            this.bookMarkView.showExtraBooks(booksResponse);
        } else if (type == 2) {
            FindResponse findResponse = (FindResponse) result.getData();
            this.findview.showFinds(findResponse.getExtra(), findResponse.getData());
        } else if (type == 3) {
            ArrayList<BannerResponse> list = (ArrayList<BannerResponse>) result.getData();
            this.view.showBanners(list);
        }else if (type==4){
            UserBuyRecordResponse recordResponse= (UserBuyRecordResponse) result.getData();
            this.bookview.showBuyHistory(recordResponse.getData());
        }else if (type==5){
            this.bookMarkView.showMallBanners((ArrayList<BannerResponse>) result.getData());
        }
    }

    @Override
    public void OnFailure(Throwable throwable, int type) {

        if (type == 0 || type == 1) {
            this.bookMarkView.ShowError(throwable);
        } else if (type == 2) {
            this.findview.ShowError(throwable);
        } else if (type == 3) {

        }else if (type==5){
            this.bookMarkView.ShowError(throwable);
        }

    }
}
